﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace E.Y.E.Jobs
{
    class CommandComplete : ICommand
    {
        private const int POSITION_INVALID_ID = -55;
        private const string MESSAGE_COMMAND_TITLE = "/=======COMPLETE COMMAND=======\\";
        private const string MESSAGE_UNDO_COMPLETE = "Undo: The task \"{0}\" is reset from Done.";
        private const string MESSAGE_SUCCESSFUL_EDIT = "The task with ID:{0} is successfully set to completed!";
        private const string ERROR_MESSAGE_FAIL_EDITING = "So embarrasing. Error occured while we are trying to change your job to complete.";

        ArrayList OriginalDatabase;
        Task CompletingTask;
        int CompletingID;
        String Message;
        bool ErrorFlag;

        public CommandComplete()
        {
            OriginalDatabase = new ArrayList();
            CompletingTask = new Task();
            CompletingID = POSITION_INVALID_ID;
            Message = "";
            ErrorFlag = true;
        }

        public bool Execute(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            bool isInvalidPosition = (CompletingID == POSITION_INVALID_ID);
            if (isInvalidPosition)
            {
                Message = ERROR_MESSAGE_FAIL_EDITING;
                return false;
            }

            try
            {
                OriginalDatabase = taskDatabase.GetCurrentList();

                CompletingTask = taskDatabase.GetSpecificTask(CompletingID);
                CompletingTask.Status = TaskEnumTypes.TaskStatus.Done;

                taskDatabase.Update(CompletingTask);

                actionsHistory.AddNewHistory(this);

                Message = MESSAGE_SUCCESSFUL_EDIT;
                ErrorFlag = false;
                return true;
            }
            catch
            {
                Message = ERROR_MESSAGE_FAIL_EDITING;
                ErrorFlag = true;
                return false;
            }
        }

        public bool Undo(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            try
            {
                bool UndoSuccess = taskDatabase.RestoreList(OriginalDatabase);
                Message = String.Format(MESSAGE_UNDO_COMPLETE, CompletingTask.Detail);
                ErrorFlag = false;
                return UndoSuccess;
            }
            catch
            {
                return false;
            }
        }

        public string getCommandEventTitle()
        {
            return MESSAGE_COMMAND_TITLE;
        }

        public void setMessage(string desiredMessage)
        {
            Message = desiredMessage;
        }

        public string getMessage()
        {
            return Message;
        }

        public void SetError(bool desiredBool)
        {
            ErrorFlag = desiredBool;
        }

        public bool GetErrorState()
        {
            return ErrorFlag;
        }

        public InformationPackage ConvertToInfoPackage()
        {
            InformationPackage Result = new InformationPackage();

            if (ErrorFlag)
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.OperationError;
            }
            else
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.Complete;
                Result.Task = CompletingTask;
            }
            Result.Message = Message;

            return Result;
        }
    }
}
